package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.PhysicalOrder;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.PhysicalOrderMapper;
import com.yami.shop.service.PhysicalOrderService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 实物订单服务实现类
 */
@Service
public class PhysicalOrderServiceImpl extends ServiceImpl<PhysicalOrderMapper, PhysicalOrder> implements PhysicalOrderService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Void> createPhysicalOrder(PhysicalOrder physicalOrder) {
        if (physicalOrder.getProductId() == null) {
            throw new YamiShopBindException("商品ID不能为空");
        }
        
        if (physicalOrder.getShippingAddress() == null || physicalOrder.getShippingAddress().isEmpty()) {
            throw new YamiShopBindException("收货地址不能为空");
        }
        
        physicalOrder.setCreateTime(new Date());
        physicalOrder.setUpdateTime(new Date());
        
        // 默认为待付款状态
        if (physicalOrder.getOrderStatus() == null) {
            physicalOrder.setOrderStatus(1);
        }
        
        save(physicalOrder);
        return ResponseEntity.ok().build();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Void> updatePhysicalOrder(PhysicalOrder physicalOrder) {
        if (physicalOrder.getId() == null) {
            throw new YamiShopBindException("订单ID不能为空");
        }
        
        if (physicalOrder.getShippingAddress() == null || physicalOrder.getShippingAddress().isEmpty()) {
            throw new YamiShopBindException("收货地址不能为空");
        }
        
        physicalOrder.setUpdateTime(new Date());
        
        updateById(physicalOrder);
        return ResponseEntity.ok().build();
    }

    @Override
    public ResponseEntity<PhysicalOrder> getPhysicalOrderById(Long id) {
        PhysicalOrder physicalOrder = getById(id);
        if (physicalOrder == null) {
            throw new YamiShopBindException("订单不存在");
        }
        return ResponseEntity.ok(physicalOrder);
    }

    @Override
    public ResponseEntity<IPage<PhysicalOrder>> listPhysicalOrdersByPage(Page page, Long shopId) {
        IPage<PhysicalOrder> physicalOrders = baseMapper.listPhysicalOrdersByPage(page, shopId);
        return ResponseEntity.ok(physicalOrders);
    }

    @Override
    public ResponseEntity<List<PhysicalOrder>> listPhysicalOrdersByBuyerId(Long buyerId) {
        List<PhysicalOrder> physicalOrders = baseMapper.listPhysicalOrdersByBuyerId(buyerId);
        return ResponseEntity.ok(physicalOrders);
    }

    @Override
    public ResponseEntity<List<PhysicalOrder>> listPhysicalOrdersByTrackingNumber(String trackingNumber) {
        List<PhysicalOrder> physicalOrders = baseMapper.listPhysicalOrdersByTrackingNumber(trackingNumber);
        return ResponseEntity.ok(physicalOrders);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Void> updateOrderStatus(Long id, Integer status) {
        PhysicalOrder physicalOrder = getById(id);
        if (physicalOrder == null) {
            throw new YamiShopBindException("订单不存在");
        }
        
        physicalOrder.setOrderStatus(status);
        updateById(physicalOrder);
        return ResponseEntity.ok().build();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Void> updateTrackingInfo(Long id, String trackingNumber) {
        PhysicalOrder physicalOrder = getById(id);
        if (physicalOrder == null) {
            throw new YamiShopBindException("订单不存在");
        }
        
        physicalOrder.setTrackingNumber(trackingNumber);
        updateById(physicalOrder);
        return ResponseEntity.ok().build();
    }
} 